﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoadTeam.Standard;
using System.Web.Script.Serialization;
using RoadTeam.BLL;
using System.Data;
using RoadTeam.Pub;
using RoadTeam.Enums;
using System.IO;
using System.Threading;

namespace RoadTeam.Pase
{
    public class PaseHomeInspection : BasePase
    {
        #region 插入验房单据集合[InsertHomeInspection]
        /// <summary>
        /// 插入验房单集合
        /// </summary>
        /// <param name="Json">验房单集合中转换后的Json</param>
        /// <returns>验房单集合(在服务器中生成唯一标识)</returns>
        public static CsHomeInspection InsertHomeInspection(String Json)
        {
            BllBase blbase = new BllBase();
            //blbase.InserLog(Json);
            blbase.InsertLog(Json, @"RoadTeam.Pase.PaseHomeInspection", " public static CsHomeInspection InsertHomeInspection(String Json)");


            CsHomeInspection Inspection = new CsHomeInspection();
            JavaScriptSerializer jvs = new JavaScriptSerializer();
            Inspection = jvs.Deserialize<CsHomeInspection>(Json);
            DataTable dt_ValidateRoom = CsValidateRoomBll.GetEmptyTable();
            DataRow dr_ValidateRoom;
            DataTable dt_ValidateRoomInfo = CsValidateRoomInfoBLL.GetEmptyTable();
            DataRow dr_ValidateRoomInfo;
            String ValidateRoomIdentifier = String.Empty;
            #region 解析成两个DataTable
            foreach (CsHomeInspection_Temp Temp in Inspection.Item)
            {
                dr_ValidateRoom = dt_ValidateRoom.NewRow();
                ValidateRoomIdentifier = CreateIdentifier.GetIdentifier();
                dr_ValidateRoom["ValidateRoomIdentifier"] = ValidateRoomIdentifier;
                dr_ValidateRoom["ValidateRoomCode"] = Temp.ValidateRoom.ValidateRoomCode;
                dr_ValidateRoom["FoundDate"] = Temp.ValidateRoom.FoundDate;
                dr_ValidateRoom["ValidateStaffIdentifier"] = Temp.ValidateRoom.ValidateStaffIdentifier;
                dr_ValidateRoom["DeveloperOpinion"] = Temp.ValidateRoom.DeveloperOpinion;
                dr_ValidateRoom["SubjoinExplain"] = Temp.ValidateRoom.SubjoinExplain;
                dr_ValidateRoom["ValidateRoomState"] = Temp.ValidateRoom.ValidateRoomState;
                dr_ValidateRoom["UnitIdentifier"] = Temp.ValidateRoom.UnitIdentifier;
                dr_ValidateRoom["OrganIdentifier"] = Temp.ValidateRoom.OrganIdentifier;
                Temp.ValidateRoom.ValidateRoomIdentifier = ValidateRoomIdentifier;
                foreach (CsValidateRoomInfo info in Temp.ValidateRoomInfo)
                {
                    dr_ValidateRoomInfo = dt_ValidateRoomInfo.NewRow();
                    dr_ValidateRoomInfo["ValidateRInfoIdentifier"] = CreateIdentifier.GetIdentifier();
                    dr_ValidateRoomInfo["ValidateRoomIdentifier"] = ValidateRoomIdentifier;
                    dr_ValidateRoomInfo["RoomTypeIdentifier"] = info.RoomTypeIdentifier;
                    dr_ValidateRoomInfo["ValidateObjectIdentifier"] = info.ValidateObjectIdentifier;
                    dr_ValidateRoomInfo["ValidateProIdentifier"] = info.ValidateProIdentifier;
                    dr_ValidateRoomInfo["ValidateState"] = info.ValidateState;
                    dr_ValidateRoomInfo["ValidateExplain"] = info.ValidateExplain;
                    dr_ValidateRoomInfo["IsRepair"] = info.IsRepair;
                    dr_ValidateRoomInfo["FillStaffIdentifier"] = info.FillStaffIdentifier;
                    dr_ValidateRoomInfo["FillDate"] = info.FillDate;
                    //dr_ValidateRoomInfo["RepairBeginDate"] =( info.RepairBeginDate==DateTime.MinValue?DBNull.Value:info.RepairBeginDate);
                    if (info.RepairBeginDate == DateTime.MinValue)
                    {
                        dr_ValidateRoomInfo["RepairBeginDate"] = DBNull.Value;
                    }
                    else
                    {
                        dr_ValidateRoomInfo["RepairBeginDate"] = info.RepairBeginDate;
                    }
                    // dr_ValidateRoomInfo["RepairEndDate"] =info.RepairEndDate;
                    if (info.RepairEndDate == DateTime.MinValue)
                    {
                        dr_ValidateRoomInfo["RepairEndDate"] = DBNull.Value;
                    }
                    else
                    {
                        dr_ValidateRoomInfo["RepairEndDate"] = info.RepairEndDate;
                    }
                    info.ValidateRInfoIdentifier = dr_ValidateRoomInfo["ValidateRInfoIdentifier"].ToString();
                    dt_ValidateRoomInfo.Rows.Add(dr_ValidateRoomInfo);
                }
                dt_ValidateRoom.Rows.Add(dr_ValidateRoom);
            }
            #endregion
            CsValidateRoomBll roombll = new CsValidateRoomBll();
            roombll.insert(dt_ValidateRoom);
            CsValidateRoomInfoBLL infobll = new CsValidateRoomInfoBLL();
            infobll.insert(dt_ValidateRoomInfo);
            return Inspection;
        }
        #endregion

        #region 获取机构下面的户型信息[GetDoorType]
        /// <summary>
        /// 获取机构下面的户型信息
        /// 参数[OrganIdentifier]:机构标识
        /// 创建时间:2013年11月4日10:19:33
        /// </summary>
        /// <param name="OrganIdentifier">机构标识</param>
        /// <returns>户型信息数据集合</returns>
        public static CsDoorType GetDoorType(String OrganIdentifier)
        {
            BllBase bllbase = new BllBase();
            //bllbase.InserLog("方法名[GetDoorType]调用时间[" + DateTime.Now.ToString() + "] 参数为:[" + OrganIdentifier + "]");
            bllbase.InsertLog(@"方法名[GetDoorType]调用时间[" + DateTime.Now.ToString() + "] 参数为:[" + OrganIdentifier + "]",
@"RoadTeam.Pase.PaseHomeInspection", "public static CsDoorType GetDoorType(String OrganIdentifier)");
            CsDoorTypeBll bll = new CsDoorTypeBll();
            DataTable dt = bll.GetDoorTypeData(OrganIdentifier);
            List<DoorType> li = DtToList<DoorType>(dt);
            CsDoorType types = new CsDoorType();
            types.Item = li.ToArray();
            return types;
        }
        #endregion

        #region 获取机构下面的房间类型信息[GetRoomTypeData]
        /// <summary>
        ///  获取机构下面的房间类型信息
        ///  参数[OrganIdentifier]:机构标识
        ///  创建时间:2013年11月4日10:18:48
        /// </summary>
        /// <param name="OrganIdentifier">机构标识</param>
        /// <returns>房间类型数据集合</returns>
        public static CsRoomType GetRoomTypeData(String OrganIdentifier)
        {
            BllBase bllbase = new BllBase();
            //bllbase.InserLog("方法名[GetRoomTypeData]调用时间[" + DateTime.Now.ToString() + "] 参数为:[" + OrganIdentifier + "]");
            bllbase.InsertLog(@"方法名[GetRoomTypeData]调用时间[" + DateTime.Now.ToString() + "] 参数为:[" + OrganIdentifier + "]",
@"RoadTeam.Pase.PaseHomeInspection", "public static CsRoomType GetRoomTypeData(String OrganIdentifier)");

            CsRoomTypeBll bll = new CsRoomTypeBll();
            DataTable dt = bll.GetRoomTypeData(OrganIdentifier);
            List<RoomType> li = DtToList<RoomType>(dt);
            CsRoomType types = new CsRoomType();
            types.Item = li.ToArray();
            return types;
        }
        #endregion

        #region 获取验收对象信息[GetValidateObjectData]
        /// <summary>
        /// 根据机构标识获取验收对象信息
        /// 参数[OrganIdentifier]:机构标识
        /// </summary>
        /// <param name="OrganIdentifier">机构标识</param>
        /// <returns>验房项目数据集合</returns>
        public static CsValidateObject GetValidateObjectData(String OrganIdentifier)
        {
            BllBase bllbase = new BllBase();
            //bllbase.InserLog("方法名[GetValidateObjectData]调用时间[" + DateTime.Now.ToString() + "] 参数为:[" + OrganIdentifier + "]");
            bllbase.InsertLog(@"方法名[GetValidateObjectData]调用时间[" + DateTime.Now.ToString() + "] 参数为:[" + OrganIdentifier + "]",
@"RoadTeam.Pase.PaseHomeInspection", "public static CsValidateObject GetValidateObjectData(String OrganIdentifier)");



            CsValidateObjectBll bll = new CsValidateObjectBll();
            DataTable dt = bll.GetValidateObjectData(OrganIdentifier);
            List<ValidateObject> li = DtToList<ValidateObject>(dt);
            CsValidateObject objects = new CsValidateObject();
            objects.Item = li.ToArray();
            return objects;
        }
        #endregion

        #region 获取验收项目信息[GetValidateProData]
        /// <summary>
        /// 根据机构获取验房项目
        /// 参数[OrganIdentifier]:机构标识
        /// 创建时间:2013年11月4日10:26:20
        /// </summary>
        /// <param name="OrganIdentifier">机构标识</param>
        /// <returns>验房项目数据集合</returns>
        public static CsValidatePro GetValidateProData(String OrganIdentifier)
        {
            BllBase bllbase = new BllBase();
            //bllbase.InserLog("方法名[GetValidateProData]调用时间[" + DateTime.Now.ToString() + "] 参数为:[" + OrganIdentifier + "]");
            bllbase.InsertLog(@"方法名[GetValidateProData]调用时间[" + DateTime.Now.ToString() + "] 参数为:[" + OrganIdentifier + "]",
@"RoadTeam.Pase.PaseHomeInspection", "public static CsValidatePro GetValidateProData(String OrganIdentifier)");


            CsValidateProBll bll = new CsValidateProBll();
            DataTable dt = bll.GetCsValidateProData(OrganIdentifier);
            List<ValidatePro> li = DtToList<ValidatePro>(dt);
            CsValidatePro pro = new CsValidatePro();
            pro.Item = li.ToArray();
            return pro;
        }
        #endregion

        #region 获取户型房间关系信息[GetDoorRoomRelaData]
        /// <summary>
        /// 获取户型房间关系数据
        /// </summary>
        /// <returns>户型房间关系</returns>
        public static CsDoorRoomRela GetDoorRoomRelaData()
        {
            BllBase bllbase = new BllBase();
            // bllbase.InserLog("方法名[GetDoorRoomRelaData]调用时间[" + DateTime.Now.ToString() + "]");
            bllbase.InsertLog(@"方法名[GetDoorRoomRelaData]调用时间[" + DateTime.Now.ToString() + "]",
@"RoadTeam.Pase.PaseHomeInspection", "public static CsDoorRoomRela GetDoorRoomRelaData()");

            CsDoorRoomRelaBll bll = new CsDoorRoomRelaBll();
            DataTable dt = bll.GetDoorRoomRelaData();
            List<DoorRoomRela> li = DtToList<DoorRoomRela>(dt);
            CsDoorRoomRela rela = new CsDoorRoomRela();
            rela.Item = li.ToArray();
            return rela;
        }
        #endregion


        #region 获取房间验收对象关系信息[GetDoorRoomRelaData]
        /// <summary>
        /// 获取房间验收对象关系信息
        /// </summary>
        /// <returns>房间验收对象关系</returns>
        public static CsRoomObjectRela GetRoomObjectRelaData()
        {
            BllBase bllbase = new BllBase();
            // bllbase.InserLog("方法名[GetRoomObjectRelaData]调用时间[" + DateTime.Now.ToString() + "]");

            bllbase.InsertLog(@"方法名[GetRoomObjectRelaData]调用时间[" + DateTime.Now.ToString() + "]",
@"RoadTeam.Pase.PaseHomeInspection", "public static CsRoomObjectRela GetRoomObjectRelaData()");
            CsRoomObjectRelaBll bll = new CsRoomObjectRelaBll();
            DataTable dt = bll.GetRoomRelaObjectData();
            List<RoomObjectRela> li = DtToList<RoomObjectRela>(dt);
            CsRoomObjectRela rela = new CsRoomObjectRela();
            rela.Item = li.ToArray();
            return rela;
        }
        #endregion


        #region 上传Json图片[UpLoadValidateFile]
        /// <summary>
        /// 上传JSON图片
        /// 参数[JSON]:包含图片转后的Json字符串
        /// 创建时间:2013年11月4日11:25:13
        /// </summary>
        /// <param name="Json">图片转后的Json字符串</param>
        /// <returns></returns>
        public static bool UpLoadValidateFile(String Json)
        {
            BllBase bllbase = new BllBase();
            //blbase.InserLog(Json);
            bllbase.InsertLog(@"方法名[UpLoadValidateFile]调用时间[" + DateTime.Now.ToString() + "]",
@"RoadTeam.Pase.PaseHomeInspection", " public static bool UpLoadValidateFile(String Json)");

            JavaScriptSerializer jvs = new JavaScriptSerializer();

            CsValidateRoomPic pic = jvs.Deserialize<CsValidateRoomPic>(Json);
            String RtSysPath = new RtParameter(RoadTeam.Enums.ParaName.AppFilePath).ParamValue;
            //String Paths = @"\\192.168.0.66\Website\Image\" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day;
            //String Paths = Server.MapPath(@"~/Copy") + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + "\\";
            String PathDate = DateTime.Now.Year.ToString() + ((DateTime.Now.Month.ToString().Length == 2) ? DateTime.Now.Month.ToString() : "0" +
                DateTime.Now.Month.ToString()) + DateTime.Now.Day.ToString();
            String Paths = GlobalConfig.FilePath + "\\" + PathDate;
            RtSysPath = RtSysPath + "/" + PathDate;
            if (!Directory.Exists(Paths + "\\" + pic.StaffName))//判断是否存在        
                Directory.CreateDirectory(Paths + "\\" + pic.StaffName);//创建新路径
            Paths = Paths + @"\" + pic.StaffName;
            RtSysPath = RtSysPath + @"/" + pic.StaffName;
            byte[] ImageByte = Convert.FromBase64String(pic.Image);
            //pic.ImageName = DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Ticks + DateTime.Now.Millisecond + @".jpg";
            try
            {
                using (FileStream fs = new FileStream(Paths + "\\" + pic.ImageName, FileMode.Create))
                {
                    fs.Write(ImageByte, 0, pic.Image.Length);
                    fs.Flush();
                    fs.Close();
                }
            }
            catch
            {
                ThreadWriteFile threadWriteFile = new ThreadWriteFile(Paths, pic.ImageName, ImageByte, pic.BusninessID, RtSysPath);
                //threadWriteFile.threadFinished += new ThreadWriteFile.ThreadFinished(threadWriteFile_threadFinished);
                Thread thread = new Thread(new ThreadStart(threadWriteFile.write));
                thread.Start();
            }
            CsValidateRoomPicBll bll = new CsValidateRoomPicBll();
            bool flag = bll.Insert(pic.BusninessID, RtSysPath, Paths, pic.ImageName);
            //判断文件是否已经保存在服务器中
            flag = FileOperate.FileExist(Paths, pic.ImageName);
            return flag;
        }
        #endregion


        /// <summary>
        /// 分页获取验房项目
        /// </summary>
        /// <param name="OrganIdentifier">所属机构标识</param>
        /// <param name="CurrentPage">当前页码</param>
        /// <param name="PageSize">页数</param>
        /// <returns>数据集合</returns>
        public static CsValidatePro2 GetValidatePro2DataByPage(String OrganIdentifier, int CurrentPage, int PageSize)
        {
            BllBase bllbase = new BllBase();
            // bllbase.InserLog("方法名[GetRoomObjectRelaData]调用时间[" + DateTime.Now.ToString() + "]");

            bllbase.InsertLog(@"方法名[GetValidatePro2DataByPage]调用时间[" + DateTime.Now.ToString() + "]",
@"RoadTeam.Pase.PaseHomeInspection", "public static CsValidatePro2 GetValidatePro2DataByPage(String OrganIdentifier, int CurrentPage, int PageSize)");

            ValidatePro2Bll bll = new ValidatePro2Bll();
            DataTable dt = bll.GetValidatePro2DataByPage(OrganIdentifier, CurrentPage, PageSize);
            List<ValidatePro2> li = DtToList<ValidatePro2>(dt);
            CsValidatePro2 pro2 = new CsValidatePro2();
            pro2.Item = li.ToArray();
            return pro2;
        }

        /// <summary>
        /// 获取验房项目的总条数
        /// </summary>
        /// <param name="OrganIdentifier">机构标识</param>
        /// <returns></returns>
        public static int GetValidateProDataAllPageCount(String OrganIdentifier)
        {
            BllBase bllbase = new BllBase();
            // bllbase.InserLog("方法名[GetRoomObjectRelaData]调用时间[" + DateTime.Now.ToString() + "]");

            bllbase.InsertLog(@"方法名[GetValidatePro2DataByPage]调用时间[" + DateTime.Now.ToString() + "]",
@"RoadTeam.Pase.PaseHomeInspection", " public int GetValidateProDataAllPageCount(String OrganIdentifier)");

            ValidatePro2Bll bll = new ValidatePro2Bll();
            return bll.GetValidateProDataAllPageCount(OrganIdentifier);
        }



    }
}
